package com.jiwei.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiwei.community.constants.SystemConstant;
import com.jiwei.community.dao.UserMapper;
import com.jiwei.community.entity.LoginUser;
import com.jiwei.community.entity.SecurityCacheUser;
import com.jiwei.community.entity.User;
import com.jiwei.community.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 季伟
 * 功能：
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Resource(type = UserMapper.class)
    UserMapper sysUserMapper;
//    @Autowired
//    MenuMapper menuMapper;
//    @Autowired
//    RoleService roleService;
    /**
     * spring会自动将此类对象注入到对应的类中中进行用户信息的获取
     * 从数据库中获取用户数据
     * @param username 用户名
     * @return 用户信息对象 该对象会返回到AuthenticationProvider中进行相应的验证处理，
     *         此处如果密码错误他会根据local也就是请求中携带的地址信息获取你的位置然后获取一个与地区相符语言的报错信息
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询用户信息
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUserName,username);
        User user = sysUserMapper.selectOne(lambdaQueryWrapper);
        if (user == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        //如果时后台的用户则获取他的权限信息否则不进行权限控制因为前台不许用限制菜单权限
        System.out.println(user.getType());
        if (SystemConstant.USER_TYPE_ADMIN.equals(user.getType())){
            return getAdminDetail(user);
        }
        SecurityCacheUser securityCacheUser = BeanCopyUtils.copyBean(user,SecurityCacheUser.class);
        return new LoginUser(securityCacheUser,null,null,user);
    }
    private List<String> emptyFilter(List<String> list){
        return list.stream()
                .filter(StringUtils::hasText)
                .collect(Collectors.toList());
    }

    private LoginUser getAdminDetail(User user){
        // 从数据库中查询用户的权限以及角色信息
        List<String> permissions = new ArrayList<>();
        List<String> roles = new ArrayList<>();
        //如果该用户是admin超级管理员则默认获取所有权限并且角色只需要一个admin
//        if (roleService.isAdminByUserId(user.getId())){
//            LambdaQueryWrapper<Menu> menuLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            menuLambdaQueryWrapper.eq(Menu::getStatus, 0);
//            permissions = menuMapper.selectList(menuLambdaQueryWrapper)
//                    .stream()
//                    .map(Menu::getPerms)
//                    .filter(StringUtils::hasText)
//                    .collect(Collectors.toList());
//            roles.add("admin");
//        }else {
//            permissions = emptyFilter(menuMapper.selectPermsByUserId(user.getId()));
//            roles = emptyFilter(roleService.selectRolesByUserId(user.getId()));
//        }
        SecurityCacheUser securityCacheUser = BeanCopyUtils.copyBean(user,SecurityCacheUser.class);
        return new LoginUser(securityCacheUser,permissions,roles,user);
    }
}
